layui.use(['table', 'admin', 'ax'], function () {
    var $ = layui.$;
    var table = layui.table;
    var $ax = layui.ax;
    var admin = layui.admin;

    /**
     * 参与作品管理
     */
    var Works = {
        tableId: "worksTable"
    };

    /**
     * 初始化表格的列
     */
    Works.initColumn = function () {
        return [[
            {type: 'checkbox'},
            {field: 'id',  title: 'id'},
            {field: 'activityTitle', sort: true, title: '活动标题'},
            {field: 'activityId', sort: true, title: '活动id'},
            {field: 'projectTypeName', sort: true, title: '作品类型'},
            {field: 'playerId', sort: true,hide: true, title: '用户id'},
            {field: 'userName', sort: true, title: '用户名称'},
            {field: 'headPic', hide: true,sort: true, title: '用户头像'},
            {field: 'text', sort: true, title: '内容'},
            {field: 'watch', sort: true, title: '浏览量'},
            {field: 'praise', sort: true, title: '点赞数',align:'center',event:'praisePage',
                templet:function (d) {
                    return '<span style="color: #01AAED;">' + d.praise + '</span>'
                }},
            {field: 'commentCount', sort: true, title: '评论数',align:'center',event:'commentPage',
                templet:function (d) {
                        return '<span style="color: #01AAED;">' + d.commentCount + '</span>'
                }
            },
            {field: 'markName', sort: true, title: '标识'},
            {field: 'sortName', sort: true, title: '获奖状态'},
            {field: 'showStatus', sort: true, title: '状态'},
            {field: 'offlineTime', sort: true,hide: true, title: '下线时间'},
            {field: 'handlePerson', sort: true,  hide: true,title: '处理人'},
            {field: 'content', sort: true, hide: true, title: '备注'},
            {field: 'handleTime', sort: true,  hide: true,title: '处理时间'},
            {field: 'createTime', sort: true, title: '新增时间'},
            {align: 'center', toolbar: '#tableBar', title: '操作',minWidth:180}
        ]];
    };

    /**
     * 点击查询按钮
     */
    Works.search = function () {
        var queryData = {};
        queryData['activityId'] = $("#activityId").val();
        queryData['userName'] = $("#userName").val();
        queryData['projectType'] = $("#projectType").val();
        queryData['status'] = $("#status").val();
        table.reload(Works.tableId, {where: queryData
            ,page:{
            current:1
        }
        });
    };

    /**
     * 弹出添加对话框
     */
    Works.openAddDlg = function () {
        admin.putTempData('formOk', false);
        top.layui.admin.open({
            type: 2,
            title: '添加参与作品',
            content: Feng.ctxPath + '/works/add',
            end: function () {
                admin.getTempData('formOk') && table.reload(Works.tableId);
            }
        });
    };

    /**
     * 导出excel按钮
     */
    Works.exportExcel = function () {
        var checkRows = table.checkStatus(Works.tableId);
        if (checkRows.data.length === 0) {
            Feng.error("请选择要导出的数据");
        } else {
            table.exportFile(tableResult.config.id, checkRows.data, 'xls');
        }
    };

    /**
     * 点击编辑
     *
     * @param data 点击按钮时候的行数据
     */
    Works.openEditDlg = function (data) {
        admin.putTempData('formOk', false);
        top.layui.admin.open({
            type: 2,
            title: '修改参与作品',
            maxmin:true,
            area:["80%","70%"],
            content: Feng.ctxPath + '/works/edit?id=' + data.id,
            end: function () {
                admin.getTempData('formOk') && table.reload(Works.tableId);
            }
        });
    };


    /**
     * 点击查看评论
     * @param data
     */
    Works.openCommentDlg = function (data) {
        admin.putTempData('formOk', false);
        top.layui.admin.open({
            type: 2,
            title: '作品评论',
            maxmin:true,
            area:["80%","70%"],
            content: Feng.ctxPath + '/comment/?id=' + data.id+"&commentType="+data.projectType,
            end: function () {
                admin.getTempData('formOk') && table.reload(Works.tableId);
            }
        });
    };
    /**
     * 点赞列表
     * @param data
     */
    Works.openPraiseDlg = function (data) {

        admin.putTempData('formOk', false);
        top.layui.admin.open({
            type: 2,
            title: '作品点赞',
            maxmin:true,
            area:["80%","70%"],
            content: Feng.ctxPath + '/works/praise?id=' + data.id+"&commentType="+data.projectType,
            end: function () {
                admin.getTempData('formOk') && table.reload(Works.tableId);
            }
        });
    };

    /**
     * 点击删除
     *
     * @param data 点击按钮时候的行数据
     */
    Works.onDeleteItem = function (data) {
        var operation = function () {
            var ajax = new $ax(Feng.ctxPath + "/works/delete", function (data) {
                Feng.success("删除成功!");
                table.reload(Works.tableId);
            }, function (data) {
                Feng.error("删除失败!" + data.responseJSON.message + "!");
            });
            ajax.set("id", data.id);
            ajax.start();
        };
        Feng.confirm("是否删除?", operation);
    };

    // 渲染表格
    var tableResult = table.render({
        elem: '#' + Works.tableId,
        url: Feng.ctxPath + '/works/list?item='+ Feng.getUrlParam("activityId")+"&type="+ Feng.getUrlParam("projectType"),
        page: true,
        height: "full-158",
        cellMinWidth: 100,
        cols: Works.initColumn()
    });

    // 搜索按钮点击事件
    $('#btnSearch').click(function () {
        Works.search();
    });

    // 添加按钮点击事件
    $('#btnAdd').click(function () {
        Works.openAddDlg();
    });

    // 导出excel
    $('#btnExp').click(function () {
        Works.exportExcel();
    });

    // 工具条点击事件
    table.on('tool(' + Works.tableId + ')', function (obj) {
        var data = obj.data;
        var layEvent = obj.event;

        if (layEvent === 'edit') {
            Works.openEditDlg(data);
        } else if (layEvent === 'delete') {
            Works.onDeleteItem(data);
        }else if(layEvent === 'commentPage'){
            if(data.commentPage=="0"){
                return false;
            }
            Works.openCommentDlg(data);

        }else if(layEvent==="praisePage"){
            //查看点赞名单
            if(data.praise=="0"){
                return false;
            }
            Works.openPraiseDlg(data);
        }
    });
});
